//
//  ZLTextField.m
//  ZLCustomViews
//
//  Created by 科大 on 2024/6/19.
//

#import "ZLTextField.h"

@interface ZLTextField()<UITextFieldDelegate>

@end

@implementation ZLTextField

- (void)awakeFromNib {
    [super awakeFromNib];
    self.delegate = self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delegate = self;
    }
    return self;
}

- (void)setStyleType:(ZLStyleType)styleType {
    _styleType = styleType;
    if (styleType == ZLStyleTypePWD) {
        self.secureTextEntry = YES;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, self.bounds.size.height);
        [button setImage:[self imageNamed:@"eye_close"] forState:UIControlStateNormal];
        [button setImage:[self imageNamed:@"eye_open"] forState:UIControlStateSelected];
        [button setTitle:@"  " forState:UIControlStateNormal];
        [button setTitle:@"  " forState:UIControlStateSelected];
        [button addTarget:self action:@selector(eyeAction:) forControlEvents:UIControlEventTouchUpInside];
        self.rightView = button;
        return;
    }
    self.rightView = nil;
}

- (void)eyeAction:(UIButton *)button {
    button.selected = !button.selected;
    self.secureTextEntry = !button.selected;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (self.maxLength > 0) {
        if ((self.text.length + string.length) > self.maxLength) {
            return NO;
        }
    }
    switch (self.inputType) {
        case ZLInputTypeNumber: {
            NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            NSRange range = [string rangeOfCharacterFromSet:nonDigitCharacterSet];
            return range.location == NSNotFound;
        }
        case ZLInputTypeLetter: {
            NSCharacterSet *nonAlphabeticCharacterSet = [[NSCharacterSet letterCharacterSet] invertedSet];
            NSRange range = [string rangeOfCharacterFromSet:nonAlphabeticCharacterSet];
            return range.location == NSNotFound;
        }
        case ZLInputTypeNumberLetter: {
            NSCharacterSet *nonAlphanumericCharacterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
            NSRange range = [string rangeOfCharacterFromSet:nonAlphanumericCharacterSet];
            return range.location == NSNotFound;
        }
        default:
            return YES;
    }
}

- (UIImage *)imageNamed:(NSString *)name {
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
    NSBundle *imageBundle = [NSBundle bundleWithPath:[frameworkBundle pathForResource:@"ZLCustomViews" ofType:@"bundle"]];
    UIImage *image = [UIImage imageNamed:name inBundle:imageBundle compatibleWithTraitCollection:nil];
    if (image == nil) {
        return [[UIImage alloc] init];
    }
    return image;
}

@end
